package com.boxfishedu.foreignkey.web;

import com.boxfishedu.foreignkey.entity.Student;
import com.boxfishedu.foreignkey.exception.ValidationException;
import com.boxfishedu.foreignkey.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lzx on 16/3/9.
 */
@RestController
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping(method = RequestMethod.POST)
    public Student addStudent(@RequestParam(required = false) String username,
                              @RequestParam(required = false) String password,
                              @RequestParam(required = false) String grade) throws ValidationException {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(grade)) {
            throw new ValidationException("参数非法");
        } else {
            return studentService.save(username, password, grade, null, null, 0, null);
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Student> getStudentJpql(@RequestParam(required = false) String key,
                                        @RequestParam(required = false, value = "subject_name") String subjectName) {
        return studentService.findStudentsContains(key, subjectName);
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public List<Student> getStudentPage(@RequestParam(required = false) String key,
                                        @RequestParam(required = false, defaultValue = "0") Integer page,
                                        @RequestParam(required = false, defaultValue = "10") Integer size) {
        return studentService.findStudentsLike(key, page,size);
    }

    @RequestMapping(value = "/grade", method = RequestMethod.GET)
    public List<String> getStudentNative(@RequestParam(required = false) String grade) {
        return studentService.nativeFindStudents(grade);
    }

}
